package cn.itcast.filter;

import javax.servlet.*;
import java.io.IOException;
// TODO: 2020/6/25

/**
 * 处理字符集编码乱码过滤器
 */


public class EncodingFilter implements Filter {
    private String enconding;

    @Override
    public void init(FilterConfig conf) throws ServletException {
         enconding = conf.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        //1.请求响应字符集
        req.setCharacterEncoding(enconding);
        resp.setContentType("text/html;charset="+ enconding);


        //2.放行
        chain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
